import 'package:animal/constant/index_constant.dart';
import 'package:animal/socket/socket_message_model/friend_message_model.dart';
import 'package:animal/socket/type_model.dart';
import 'package:animal/ui/friend/page/friend_info_page.dart';
import 'package:animal/utils/index.dart';
import 'package:animal/utils/screen_util.dart';
import 'package:animal/utils/sputils/sp_config.dart';
import 'package:animal/widgets/HeaderWidget.dart';
import 'package:animal/widgets/image_loading.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class MessageHistoryItem extends StatelessWidget {
  final FriendMessageModel model;

  MessageHistoryItem({Key? key, required this.model}) : super(key: key);

  final Map userInfo = jsonDecode(SpConfig().getUserInfo()!);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: userInfo["id"] == model.m!.f ? _mineMessage() : _otherMessage(),
      padding: EdgeInsets.all(ScreenUtil().L(15)),
    );
  }

  ///我的消息
  _mineMessage() {
    return Row(
      children: [
        Container(
          alignment: Alignment.centerRight,
          padding: EdgeInsets.all(model.m!.mt == 0 ? ScreenUtil().L(20) : 0),
          decoration: BoxDecoration(
            color: model.m!.mt == 0 ? Colors.white : Colors.transparent,
          ),
          child: _messageContentWidget(),
        ),
        Container(width: ScreenUtil().L(20)),
        InkWell(
            child: Header.defaultHead(width: 80, imageUrl: userInfo["headImg"] ?? "", radius: 80),
            onTap: () {
              Get.to(FriendInfoPage(
                  userModel: UserModel(
                    id: userInfo["id"].toString(),
                    nickName: userInfo["nickName"],
                    uid: userInfo["uid"],
                    account: userInfo["account"],
                    gender: userInfo["sex"],
                    head: userInfo["headImg"] ?? "",
                  )));
            }),
      ],
    );
  }

  ///其他的消息
  _otherMessage() {
    return Row(
      children: [
        InkWell(
            child: Header.defaultHead(width: 80, imageUrl: userInfo["headImg"] ?? "", radius: 80),
            onTap: () {
              Get.to(FriendInfoPage(
                  userModel: UserModel(
                    id: userInfo["id"].toString(),
                    nickName: userInfo["nickName"],
                    uid: userInfo["uid"],
                    account: userInfo["account"],
                    gender: userInfo["sex"],
                    head: userInfo["headImg"] ?? "",
                  )));
            }),
        Container(width: ScreenUtil().L(20)),
        Container(
          alignment: Alignment.centerLeft,
          padding: EdgeInsets.all(model.m!.mt == 0 ? ScreenUtil().L(20) : 0),
          decoration: BoxDecoration(
            color: model.m!.mt == 0 ? Colors.white : Colors.transparent,
          ),
          child: _messageContentWidget(),
        ),
      ],
    );
  }

  _messageContentWidget() {
    if (model.m!.mt == 0) {
      return Text(model.m!.c ?? "", style: KFontConstant.myTextStyle(),);
    } else if (model.m!.mt == 1) {
      {
        return InkWell(
          onTap: () {
            // Get.to(PhotoViewSimpleScreen(imageProvider: [NetworkImage(imageEntity.url)],
            //   heroTag: 'simple',
            //   backgroundDecoration: BoxDecoration(borderRadius: BorderRadius.circular(ScreenUtil().L(10))), loadingChild: Container(),));
          },
          child: ClipRRect(
              borderRadius: BorderRadius.all(Radius.circular(ScreenUtil().L(5))),
              child: CachedNetworkImage(
                placeholder: (context, url) => const ImageLoadingWidget(),
                errorWidget: (context, url, error) => const Icon(Icons.error),
                imageUrl: "",
                width: ScreenUtil().L(20 * 1.0),
                memCacheWidth: 500,
              )),
        );
      }
    }
  }
}
